// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package com.geniusai.aip.auth.config;

import com.geniusai.aip.auth.constants.LoadConstants;
import com.geniusai.aip.auth.service.StorageService;
import com.geniusai.aip.auth.service.impl.EdsServiceImpl;
import com.geniusai.aip.auth.service.impl.NfsServiceImpl;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author longping58607
 * @date 2020/12/11
 */
@Data
@Configuration
public class ExternalConfig {
    private static final String NFS_STORAGE = "Minio";

    private static final String EDS_STORAGE = "EDS";
    /**
     * 配置web数据库feature_web
     */
    @Value("${spring.datasource.webDatabase}")
    private String webDatabase;

    /**
     * 配置train数据库training_manange
     */
    @Value("${spring.datasource.trainingDatabase}")
    private String trainingDatabase;

    /**
     * 配置数据库host
     */
    @Value("${spring.datasource.host}")
    private String dbHost;

    /**
     * 配置数据库port
     */
    @Value("${spring.datasource.port}")
    private String dbPort;

    /**
     * 配置数据库aip
     */
    @Value("${spring.datasource.aip}")
    private String aipDatabase;

    /**
     * 配置数据库username
     */
    @Value("${spring.datasource.username}")
    private String dbUsername;

    /**
     * 配置数据库password
     */
    @Value("${spring.datasource.password}")
    private String dbPassword;

    /**
     * 配置获取秘钥的密码
     */
    @Value("${key-store.password}")
    private String keypassword;

    /**
     * 配置获取秘钥的别名
     */
    @Value("${key-store.alias}")
    private String alias;

    /**
     * 配置获取认证的客户端ID
     */
    @Value("${auth.clientid}")
    private String clientid;

    /**
     * 配置获取认证的客户端秘钥
     */
    @Value("${auth.secret}")
    private String secret;

    /**
     * 配置获取accesstoken的有效期
     */
    @Value("${expiredate.accesstokenSeconds}")
    private int accesstokenSeconds;

    /**
     * 配置获取refershtoken的有效期
     */
    @Value("${expiredate.refershtokenSeconds}")
    private int refershtokenSeconds;

    /**
     * 初始的admin用户密码
     */
    @Value("${security.admin.password}")
    private String adminPwd;

    /**
     * 初始的admin用户名称
     */
    @Value("${security.admin.username}")
    private String adminName;

    /**
     * ngsoc的账号
     */
    @Value("${security.ngsoc.username}")
    private String ngsocName;

    /**
     * 认证ngsoc的验证码
     */
    @Value("${security.ngsoc.code}")
    private String ngsocCode;

    /**
     * ngsoc的初始密码
     */
    @Value("${security.ngsoc.password}")
    private String ngsocPwd;

    /**
     * 配置web访问地址
     */
    @Value("${spring.web-service.url}")
    private String webService;

    /**
     * 配置web的端口
     */
    @Value("${spring.web-service.port}")
    private String webServicePort;

    /**
     * 初始化的cpu大小
     */
    @Value("${security.config.cpu}")
    private int cpuNum;

    /**
     * 初始化的gpu大小
     */
    @Value("${security.config.vgpu}")
    private int vGpuNum;

    /**
     * 初始化的gpu大小
     */
    @Value("${security.config.vgpu-type}")
    private String vGpuType;

    /**
     * 初始化的memory大小
     */
    @Value("${security.config.memory}")
    private int memorySize;

    /**
     * 初始化的storage大小
     */
    @Value("${security.config.storage}")
    private int storageSize;

    /**
     * 配置prometheus访问地址
     */
    @Value("${spring.prometheus.url}")
    private String prometheusService;

    /**
     * 配置prometheus的端口
     */
    @Value("${spring.prometheus.port}")
    private String prometheusPort;

    /**
     * 配置grafana访问地址
     */
    @Value("${spring.grafana.url}")
    private String grafanaService;

    /**
     * 配置grafana的端口
     */
    @Value("${spring.grafana.port}")
    private String grafanaPort;

    /**
     * 配置orion-root-gui访问地址
     */
    @Value("${spring.orion-root-gui.url}")
    private String orionRootGuiService;

    /**
     * 配置orion-root-gui的端口
     */
    @Value("${spring.orion-root-gui.port}")
    private int orionRootGuiPort;

    /**
     * 配置orion-controller访问地址
     */
    @Value("${spring.orion-controller.url}")
    private String orionControllerService;

    /**
     * 配置orion-controller的端口
     */
    @Value("${spring.orion-controller.port}")
    private int orionControllerPort;

    /**
     * 配置code-studio访问地址
     */
    @Value("${spring.code-studio.url}")
    private String codeStudioService;

    /**
     * 配置code-studio的端口
     */
    @Value("${spring.code-studio.port}")
    private String codeStudioServicePort;

    /**
     * 配置training访问地址
     */
    @Value("${spring.training-service.url}")
    private String trainingService;

    /**
     * 配置training的端口
     */
    @Value("${spring.training-service.port}")
    private String trainingServicePort;

    /**
     * 配置deploy访问地址
     */
    @Value("${spring.service-manage.url}")
    private String serviceManage;

    /**
     * 配置deploy的端口
     */
    @Value("${spring.service-manage.port}")
    private String serviceManagePort;

    /**
     * 配置deploy访问地址
     */
    @Value("${spring.minio-service.url}")
    private String minioService;

    /**
     * 配置deploy的端口
     */
    @Value("${spring.minio-service.port}")
    private String minioServicePort;

    /**
     * 配置minio access key
     */
    @Value("${spring.minio-service.access-key}")
    private String minioAccessKey;

    /**
     * 配置minio secret key
     */
    @Value("${spring.minio-service.secret-key}")
    private String minioSecretKey;

    /**
     * 配置object storage
     */
    @Value("${spring.object-storage.type}")
    private String objectStorageType;

    /**
     * 配置eds url
     */
    @Value("${spring.eds-service.url}")
    private String edsServiceUrl;

    /**
     * 配置eds port
     */
    @Value("${spring.eds-service.port}")
    private String edsServicePort;

    /**
     * 配置eds username
     */
    @Value("${spring.eds-service.username}")
    private String edsServiceUserName;

    /**
     * 配置eds password
     */
    @Value("${spring.eds-service.password}")
    private String edsServicePassword;

    /**
     * 配置eds mode
     */
    @Value("${spring.eds-service.mode}")
    private String edsServiceMode;

    /**
     * 配置eds owner
     */
    @Value("${spring.eds-service.owner}")
    private String edsServiceOwner;

    /**
     * 配置eds owner
     */
    @Value("${spring.eds-service.access-key}")
    private String edsServiceAccessKey;

    /**
     * 配置eds owner
     */
    @Value("${spring.eds-service.secrete-key}")
    private String edsServiceSecreteKey;

    /**
     * 配置harbor访问地址
     */
    @Value("${spring.harbor-service.url}")
    private String harborService;

    /**
     * 配置harbor的端口
     */
    @Value("${spring.harbor-service.port}")
    private String harborPort;

    @Bean
    public StorageService resourceType() {
        if (NFS_STORAGE.equals(objectStorageType)) {
            return new NfsServiceImpl();
        } else {
            return new EdsServiceImpl();
        }
    }

    @Bean
    public Map<Integer, Map<String, List<String>>> rolePermission() {

        List<String> adminPost = new ArrayList<String>() {
            {
                add("/web");
                add("/notebook");
                add("/training");
                add("/auth");
                add("/notebooks");
                add("/deploy");
            }
        };
        Map<String, List<String>> adminMethod = new HashMap<String, List<String>>(4) {
            {
                put("post", adminPost);
                put("delete", adminPost);
                put("get", adminPost);
                put("put", adminPost);
                put("patch", adminPost);
            }
        };

        List<String> normalPost = new ArrayList<String>() {
            {
                add("/web/v1/feature/graph/load/{graphId}");
                add("/web/v1/feature/graph/sql");
                add("/web/v1/project/{projectId}/feature/graph/commit");
                add("/web/v1/project/{projectId}/feature/graph/push");
                add("/web/v1/dataSource/connection/test");
                add("/web/v1/user/{userId}/dataSource");
                add("/web/v1/user/{userId}/dataSource/file");
                add("/web/v1/streaming/job/{jobId}/status/offline");
                add("/web/v1/streaming/job/{jobId}/status/online");
                add("/web/v1/streaming/project/{projectId}/job");
                add("/web/v1/user/{userId}/image");
                add("/web/v1/user/{userId}/project");
                add("/training/v1/user/{userId}/");
                add("/training/v1/task/job/{resourceId}");
                add("/training/v1/flow/{resourceId}");
                add("/training/v1/algorithmType/{resourceId}");
                add("/notebooks/v1/users/{userId}/");
                add("/notebook/{userId}/");

                add("/deploy/v1/users/{userId}/");
                add("/deploy/v1/projects/{resourceId}");

            }
        };
        List<String> normalGet = new ArrayList<String>() {
            {
                add("/web/v1/feature/graph/{graphRuleId}");
                add("/web/v1/feature/graph/analysis");
                add("/web/v1/feature/graph/project/{projectId}");
                add("/web/v1/feature/graph/project/{projectId}/info");
                add("/web/v1/dataSource/{dataSourceId}/file");
                add("/web/v1/dataSource/{dataSourceId}/name");
                add("/web/v1/dataSource/{dataSourceId}/table");
                add("/web/v1/dataSource/esquery/{dataSourceId}/indices/{indexName}/fields");
                add("/web/v1/dataSource/esquery/{dataSourceId}/indices/{indexName}/timeFields");
                add("/web/v1/user/{userId}/dataSource");
                add("/web/v1/user/{userId}/dataSource:count");
                add("/web/v1/user/{userId}/dataSource/{dataSourceType}");
                add("/web/v1/user/{userId}/dataSource/type/{typeId}");
                add("/web/v1/streaming/{projectId}/job");
                add("/web/v1/streaming/job/{jobId}");
                add("/web/v1/user/{userId}/image");
                add("/web/v1/user/{userId}/image:count");
                add("/web/v1/user/{userId}/image/list");
                add("/web/v1/projectlist/show/{projectId}");
                add("/web/v1/project/{projectId}/experiment");
                add("/web/v1/user/{userId}/project");
                add("/web/v1/user/{userId}/project:count");
                add("/training/v1/project/{resourceId}");
                add("/training/v1/model/file/{resourceId}");
                add("/training/v1/task/job/{resourceId}");
                add("/training/v1/user/{userId}/");
                add("/training/v1/flow/{resourceId}");
                add("/training/v1/flow/-/snapshot/{resourceId}");
                add("/training/v1/algorithmType/{resourceId}");
                add("/training/v1/algorithm/{resourceId}");
                add("/training/v1/algorithm/autoAlgorithm");

                add("/notebooks/v1/users/{userId}/");
                add("/notebooks/v1/notebooks/{resourceId}");
                add("/notebook/{userId}/");

                add("/deploy/v1/users/{userId}/");
                add("/deploy/v1/models/{resourceId}");
                add("/deploy/v1/projects/{resourceId}");

            }
        };
        List<String> normalDelete = new ArrayList<String>() {
            {
                add("/web/v1/feature/graph");
                add("/web/v1/feature/graph/{graphId}");
                add("/web/v1/datasource/{dataSourceId}");
                add("/web/v1/streaming/job/{jobId}");
                add("/web/v1/image/{imageId}");
                add("/web/v1/project/{projectId}");
                add("/training/v1/model/{resourceId}");
                add("/training/v1/task/{resourceId}");
                add("/training/v1/user/{userId}/");
                add("/training/v1/flow/{resourceId}");
                add("/training/v1/algorithmType/{resourceId}");
                add("/training/v1/algorithm/{resourceId}");

                add("/notebooks/v1/notebooks/{resourceId}");
                add("/notebooks/v1/pvcs/{resourceId}");
                add("/notebook/{userId}/");

                add("/deploy/v1/images/{resourceId}");
                add("/deploy/v1/projects/{resourceId}");
                add("/deploy/v1/services/{resourceId}");
                add("/deploy/v1/grayreleases/{resourceId}");

            }
        };
        List<String> normalPut = new ArrayList<String>() {
            {
                add("/web/v1/streaming/job/{jobId}");
                add("/web/v1/image/{imageId}");
                add("/web/v1/project/{projectId}");
                add("/auth/v1/user/{userId}/");
                add("/training/v1/flow/{resourceId}");
                add("/training/v1/algorithmType/{resourceId}");
                add("/training/v1/algorithm/{resourceId}");

                add("/notebook/{userId}/");
            }
        };
        List<String> normalPatch = new ArrayList<String>() {
            {
                add("/notebooks/v1/notebooks/{resourceId}");
                add("/notebook/{userId}/");

                add("/deploy/v1/images/{resourceId}");
                add("/deploy/v1/services/{resourceId}");
                add("/deploy/v1/grayreleases/{resourceId}");

            }
        };

        Map<String, List<String>> normalMethod = new HashMap<String, List<String>>(4) {
            {
                put("post", normalPost);
                put("delete", normalDelete);
                put("get", normalGet);
                put("put", normalPut);
                put("patch", normalPatch);
            }
        };

        return new HashMap<Integer, Map<String, List<String>>>(4) {
            {
                put(LoadConstants.ADMIN, adminMethod);
                put(LoadConstants.NORMAL, normalMethod);
            }
        };
    }
}
